package View;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.JSeparator;
import java.awt.Toolkit;

public class ClientListScreen {

	public JFrame frmClientList;
	private static JTable table;
	private static ClientManager manager;
	private static Vector cli;
	public static Vector tempClientBefore; //~ client truoc khi thay doi
	private static String file = "ClientList.xml";
	
	public static int loadListOn = 1; //client list dang duoc load vao table
									//ko cho phep event table changed hoat dong

	/**
	 * Create the application.
	 */
	public ClientListScreen() {
		manager = new ClientManager();
		cli = new Vector();
		tempClientBefore = new Vector();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		frmClientList = new JFrame();
		frmClientList.setVisible(true);
		frmClientList.setIconImage(Toolkit.getDefaultToolkit().getImage("res\\view-list-icon.png"));
		frmClientList.setTitle("Client List");
		frmClientList.setBounds(100, 100, 473, 310);
		SpringLayout springLayout = new SpringLayout();
		frmClientList.getContentPane().setLayout(springLayout);
		
		JToolBar toolBar = new JToolBar();
		springLayout.putConstraint(SpringLayout.NORTH, toolBar, 0, SpringLayout.NORTH, frmClientList.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, toolBar, 0, SpringLayout.WEST, frmClientList.getContentPane());
		springLayout.putConstraint(SpringLayout.SOUTH, toolBar, 50, SpringLayout.NORTH, frmClientList.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, toolBar, 0, SpringLayout.EAST, frmClientList.getContentPane());
		frmClientList.getContentPane().add(toolBar);
		
		JButton btnAdd = new JButton("");
		btnAdd.setToolTipText("Add");
		btnAdd.setIcon(new ImageIcon("res\\Actions-edit-add-icon.png"));
		btnAdd.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				AddClientScreen add = new AddClientScreen();
			}
			
		});
		toolBar.add(btnAdd);
		
		JButton btnDeleteButton = new JButton("");
		btnDeleteButton.setToolTipText("Delete");
		btnDeleteButton.setIcon(new ImageIcon("res\\Actions-button-cancel-icon.png"));
		btnDeleteButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				DefaultTableModel model = (DefaultTableModel) table.getModel();
				int selectedRow = table.getSelectedRow();
				String ip = (String) model.getValueAt(selectedRow, 0);
				if(manager.DeleteClient(file, ip)){
					JOptionPane.showMessageDialog(frmClientList, "Delete client. Success!");
				}else{
					JOptionPane.showMessageDialog(frmClientList, "Delete fail!");
				}
				loadListOn = 1;
				ViewAll();
			}
			
		});
		toolBar.add(btnDeleteButton);
		
		JSeparator separator = new JSeparator();
		springLayout.putConstraint(SpringLayout.NORTH, separator, 55, SpringLayout.NORTH, toolBar);
		springLayout.putConstraint(SpringLayout.SOUTH, separator, 57, SpringLayout.NORTH, frmClientList.getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, separator, 0, SpringLayout.WEST, frmClientList.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, separator, 0, SpringLayout.EAST, toolBar);
		frmClientList.getContentPane().add(separator);
		
		JScrollPane scrollPane = new JScrollPane();
		springLayout.putConstraint(SpringLayout.NORTH, scrollPane, 6, SpringLayout.SOUTH, separator);
		springLayout.putConstraint(SpringLayout.WEST, scrollPane, 0, SpringLayout.WEST, toolBar);
		springLayout.putConstraint(SpringLayout.SOUTH, scrollPane, 0, SpringLayout.SOUTH, frmClientList.getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, scrollPane, 0, SpringLayout.EAST, toolBar);
		frmClientList.getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"User", "Password"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		model.addTableModelListener(new TableModelListener(){

			@Override
			public void tableChanged(TableModelEvent e) {
				// TODO Auto-generated method stub
				if(loadListOn != 1){
					int firstRow = e.getFirstRow();
					int lastRow = e.getLastRow();
					int col = e.getColumn();
					int colUser = table.getColumnModel().getColumnIndex("User");
					int colPass = table.getColumnModel().getColumnIndex("Password");
					if(col != -1){
						for(int i = firstRow; i <= lastRow; i++){
							TableModel model = (TableModel)e.getSource();
							Object data = model.getValueAt(i, col);
							System.out.println("Data la:" + data);
							if(data != null && !data.equals("")){
								if(col == colUser){
									Client cliBefore = (Client)tempClientBefore.elementAt(i); 
									manager.UpdateClientInformation(file, cliBefore.getUser(), (String)data, null);
								}
								if(col == colPass){
									Client cliBefore = (Client)tempClientBefore.elementAt(i); 
									manager.UpdateClientInformation(file, cliBefore.getUser(), null, (String)data);
								}
								tempClientBefore = manager.ViewClientList(file);
							}
						}
					}
				}
			}
			
		});
		
		table.getTableHeader().setReorderingAllowed(false);
		scrollPane.setViewportView(table);
		ViewAll();
	}
	
	public static void ViewAll(){
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		for(int i = model.getRowCount() - 1; i >= 0 ; i--){
			model.removeRow(i);
		}
		
		cli = manager.ViewClientList(file);
		tempClientBefore = manager.ViewClientList(file);

		for(int i = 0; i < cli.size(); i++){
			Client client = (Client)cli.elementAt(i);
			String user = client.getUser();
			String password = client.getPassword();
			System.out.println(model.getRowCount());
			model.insertRow(model.getRowCount(), new Object[]{user, password});
		}
		loadListOn = 0;
	}
}
